home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap21 / ShowBit / ShowBit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.9 KB  |  133 lines

  1. /*-----------------------------------------------------------
  2.    SHOWBIT.C -- Shows bitmaps in BITLIB dynamic-link library
  3.                 (c) Charles Petzold, 1998
  4.   -----------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. TCHAR szAppName [] = TEXT ("ShowBit") ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      HWND     hwnd ;
  16.      MSG      msg ;
  17.      WNDCLASS wndclass ;
  18.      
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, 
  38.                           TEXT ("Show Bitmaps from BITLIB (Press Key)"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      if (!hwnd)
  45.           return 0 ;
  46.      
  47.      ShowWindow (hwnd, iCmdShow) ;
  48.      UpdateWindow (hwnd) ;
  49.      
  50.      while (GetMessage (&msg, NULL, 0, 0))
  51.      {
  52.           TranslateMessage (&msg) ;
  53.           DispatchMessage (&msg) ;
  54.      }
  55.      return msg.wParam ;
  56. }
  57.  
  58. void DrawBitmap (HDC hdc, int xStart, int yStart, HBITMAP hBitmap)
  59. {
  60.      BITMAP bm ;
  61.      HDC    hMemDC ;
  62.      POINT  pt ;
  63.      
  64.      hMemDC = CreateCompatibleDC (hdc) ;
  65.      SelectObject (hMemDC, hBitmap) ;
  66.      GetObject (hBitmap, sizeof (BITMAP), &bm) ;
  67.      pt.x = bm.bmWidth ;
  68.      pt.y = bm.bmHeight ;
  69.      
  70.      BitBlt (hdc, xStart, yStart, pt.x, pt.y, hMemDC, 0, 0, SRCCOPY) ;
  71.      
  72.      DeleteDC (hMemDC) ;
  73. }
  74.  
  75. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  76. {
  77.      static HINSTANCE hLibrary ;
  78.      static int       iCurrent = 1 ;
  79.      HBITMAP          hBitmap ;
  80.      HDC              hdc ;
  81.      PAINTSTRUCT      ps ;
  82.      
  83.      switch (message)
  84.      {
  85.      case WM_CREATE:
  86.           if ((hLibrary = LoadLibrary (TEXT ("BITLIB.DLL"))) == NULL)
  87.           {
  88.                MessageBox (hwnd, TEXT ("Can't load BITLIB.DLL."),
  89.                            szAppName, 0) ;
  90.                return -1 ;
  91.           }
  92.           return 0 ;
  93.           
  94.      case WM_CHAR:
  95.           if (hLibrary)
  96.           {
  97.                iCurrent ++ ;
  98.                InvalidateRect (hwnd, NULL, TRUE) ;
  99.           }
  100.           return 0 ;
  101.           
  102.      case WM_PAINT:
  103.           hdc = BeginPaint (hwnd, &ps) ;
  104.           
  105.           if (hLibrary)
  106.           {
  107.                hBitmap = LoadBitmap (hLibrary, MAKEINTRESOURCE (iCurrent)) ;
  108.  
  109.                if (!hBitmap) 
  110.                {
  111.                     iCurrent = 1 ;
  112.                     hBitmap = LoadBitmap (hLibrary, 
  113.                                           MAKEINTRESOURCE (iCurrent)) ;
  114.                }
  115.                if (hBitmap)
  116.                {
  117.                     DrawBitmap (hdc, 0, 0, hBitmap) ;
  118.                     DeleteObject (hBitmap) ;
  119.                }
  120.           }
  121.           EndPaint (hwnd, &ps) ;
  122.           return 0 ;
  123.           
  124.      case WM_DESTROY:
  125.           if (hLibrary)
  126.                FreeLibrary (hLibrary) ;
  127.           
  128.           PostQuitMessage (0) ;
  129.           return 0 ;
  130.      }
  131.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  132. }
  133.